package com.relife.core.easyui;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import com.relife.common.bean.ExcelCell;
import com.relife.common.bean.ExcelParam;
import com.relife.common.model.ClientManager;
import com.relife.common.utils.StringUtil;
import com.relife.core.entity.CodeEntity;
import org.springframework.beans.BeanUtils;

import com.alibaba.fastjson.JSONObject;
import com.relife.core.utils.ExcelUtil;

@SuppressWarnings("serial")
public class DataGridTag extends TagSupport {
	private static final String DATA_BEGIN = "_begin";
	private static final String DATA_END = "_end";
	private String name;
	private String title;
	private String idField;
	private Integer pageSize = 20;
	private boolean fit = true;
	private boolean border;
	private boolean pagination = true;
	private boolean rownumbers = true;
	private Integer width;
	private Integer height;
	private boolean checkbox = false;
	private boolean singleSelect = true;
	private boolean fitColumns = true;
	private boolean showFooter = false;
	private boolean striped = true;
	private String method = "post";
	private String sortName;
	private String sortOrder;
	private String url;
	private String toolbar;
	private String queryParams;
	private boolean treegrid;
	private String treeField;
	private boolean excel = false;
	/*** 扩展属性 */
	private String extendProperties;

	private String onLoadSuccess;
	private String onBeforeLoad;
	private String onClickRow;
	private String onDblClickRow;
	private String onClickCell;
	private String onDblClickCell;
	private String rowStyler;
	/*** 扩展函数 */
	private String extendFunction;

	private List<DataGridRowTag> row = new ArrayList<DataGridRowTag>();
	private List<DataGridRowTag> frozens = new ArrayList<DataGridRowTag>();
	private List<DataGridButTag> buts = new ArrayList<DataGridButTag>();
	private List<DataGridQueryTag> querys = new ArrayList<DataGridQueryTag>();
	private List<DataGridOptTag> opts = new ArrayList<DataGridOptTag>();
	private String fields = null;
	private String param = null;
	private String function = "";

	@Override
	public int doStartTag() throws JspTagException {
		this.fields = null;
		this.param = null;
		this.function = "";
		this.row.clear();
		this.frozens.clear();
		this.buts.clear();
		this.querys.clear();
		this.opts.clear();
		return EVAL_BODY_INCLUDE;
	}

	@Override
	public int doEndTag() throws JspException {
		try {
			JspWriter out = this.pageContext.getOut();
			StringBuffer sb = new StringBuffer();
			sb.append(getTable());
			sb.append(getToolbars());
			sb.append(getEasyuiJs());
			out.print(sb);
			out.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return EVAL_PAGE;
	}

	private StringBuffer getTable() {
		List<ExcelCell> cells = new ArrayList<ExcelCell>();
		if (this.opts.size() > 0) {
			DataGridRowTag column = new DataGridRowTag();
			column.setField("opt");
			column.setName("操作");
			column.setFormatter("formatterOpt");
			this.row.add(column);
		}
		StringBuffer sb = new StringBuffer();
		sb.append("<table id='" + this.name + "' style=\"display:none\">\n");
		if (frozens != null && frozens.size() > 0) {
			sb.append("<thead data-options='frozen:true'>\n");
			sb.append("<tr>\n");

			if (checkbox) {
				sb.append("<th data-options=\"field:'ck',checkbox:true\"></th>\n");
			}

			for (int i = 0; i < frozens.size(); i++) {

				if (this.excel && frozens.get(i).isExcel()) {
					ExcelCell cell = new ExcelCell(frozens.get(i).getName(), frozens.get(i).getField());
					cell.setCode(frozens.get(i).getCode());
					cell.setJson(frozens.get(i).getJson());
					cells.add(cell);
				}

				sb.append("<th data-options=\"field:'" + frozens.get(i).getField() + "',");
				if (StringUtil.isNotEmpty(frozens.get(i).getAlign())) {
					sb.append("align:'" + frozens.get(i).getAlign() + "',");
				}

				if (StringUtil.isNotEmpty(frozens.get(i).getCellStyler())) {
					sb.append("styler:" + frozens.get(i).getCellStyler() + ",");
				}

				if (frozens.get(i).getColspan() != null) {
					sb.append("colspan:'" + frozens.get(i).getColspan() + "',");
				}

				if (StringUtil.isNotEmpty(frozens.get(i).getFormatter())) {
					sb.append("formatter:" + frozens.get(i).getFormatter() + ",");
				}

				if (frozens.get(i).getRowspan() != null) {
					sb.append("rowspan:'" + frozens.get(i).getRowspan() + "',");
				}

				if (frozens.get(i).getWidth() != null) {
					sb.append("width:'" + frozens.get(i).getWidth() + "',");
				}

				if (StringUtil.isNotEmpty(frozens.get(i).getJson())) {
					sb.append("formatter:format" + frozens.get(i).getField() + ",");
					this.setFuncion(frozens.get(i).getField(), frozens.get(i).getJson());
				}
				if (frozens.get(i).getCode() != null) {
					sb.append("formatter:formatCode" + frozens.get(i).getField() + ",");
					List<CodeEntity> list = ClientManager.getInstance().getCodeList(frozens.get(i).getCode());
					JSONObject obj = new JSONObject();
					if (list != null) {
						for (int j = 0; j < list.size(); j++) {
							obj.put(list.get(j).getValue(), list.get(j).getName());
						}
					}
					this.setFuncion("Code" + frozens.get(i).getField(), obj.toString());
				}

				sb.append("hidden:" + frozens.get(i).isHidden() + ",");
				sb.append("sortable:" + frozens.get(i).isSortable() + ",");
				sb.append("frozen:true,");
				sb.append("sortable:" + frozens.get(i).isSortable() + "\">" + frozens.get(i).getName() + "</th>\n");
			}
			sb.append("</tr>\n");
			sb.append("</thead>\n");
		}
		if (this.row != null && this.row.size() > 0) {
			sb.append("<thead>\n");
			sb.append("<tr>\n");

			if (checkbox) {
				sb.append("<th data-options=\"field:'ck',checkbox:true\"></th>\n");
			}

			for (int i = 0; i < row.size(); i++) {

				if (this.excel && row.get(i).isExcel()) {
					ExcelCell cell = new ExcelCell(row.get(i).getName(), row.get(i).getField());
					cell.setCode(row.get(i).getCode());
					cell.setJson(row.get(i).getJson());
					cells.add(cell);
				}

				sb.append("<th data-options=\"field:'" + row.get(i).getField() + "',");
				if (StringUtil.isNotEmpty(row.get(i).getAlign())) {
					sb.append("align:'" + row.get(i).getAlign() + "',");
				}

				if (StringUtil.isNotEmpty(row.get(i).getCellStyler())) {
					sb.append("styler:" + row.get(i).getCellStyler() + ",");
				}

				if (row.get(i).getColspan() != null) {
					sb.append("colspan:'" + row.get(i).getColspan() + "',");
				}

				if (StringUtil.isNotEmpty(row.get(i).getFormatter())) {
					sb.append("formatter:" + row.get(i).getFormatter() + ",");
				}

				if (row.get(i).getRowspan() != null) {
					sb.append("rowspan:'" + row.get(i).getRowspan() + "',");
				}

				if (row.get(i).getWidth() != null) {
					sb.append("width:'" + row.get(i).getWidth() + "',");
				}
				if (StringUtil.isNotEmpty(row.get(i).getJson())) {
					sb.append("formatter:format" + row.get(i).getField() + ",");
					this.setFuncion(row.get(i).getField(), row.get(i).getJson());
				}
				if (row.get(i).getCode() != null) {
					sb.append("formatter:formatCode" + row.get(i).getField().replaceAll("\\.", "") + ",");
					List<CodeEntity> list = ClientManager.getInstance().getCodeList(row.get(i).getCode());
					JSONObject obj = new JSONObject();
					if (list != null) {
						for (int j = 0; j < list.size(); j++) {
							obj.put(list.get(j).getValue(), list.get(j).getName());
						}
					}
					this.setFuncion("Code" + row.get(i).getField().replaceAll("\\.", ""), obj.toString());
				}
				sb.append("hidden:" + row.get(i).isHidden() + ",");
				sb.append("sortable:" + row.get(i).isSortable() + "\">" + row.get(i).getName() + "</th>\n");
			}
			sb.append("</tr>\n");
			sb.append("</thead>\n");
		}
		sb.append("</table>\n");

		if (excel) {
			ExcelParam ep = new ExcelParam();
			ep.setCells(cells);
			ExcelUtil.exportExcel(ep, this.name);
		}

		return sb;
	}

	public void setFuncion(String funName, String json) {
		StringBuffer sb = new StringBuffer();
		sb.append("function format" + funName + "(val,row){\n");
		sb.append(" var map=$.parseJSON('" + json.replace("'", "\"") + "'),ret='';\n");
		sb.append(" if(val && isNaN(val) && val.indexOf(',')>0){\n");
		sb.append("  var vals=val.split(\",\");\n");
		sb.append("  for(var i=0;i<vals.length;i++){\n");
		sb.append("    ret+=(i==0 ? '' : ',')+(map[vals[i]] ? map[vals[i]] : vals[i]);\n");
		sb.append("  }\n");
		sb.append("}else{\n");
		sb.append(" ret=(map[val] ? map[val] : val);\n");
		sb.append("}\n");
		sb.append(" return ret;\n");
		sb.append("}\n");
		this.function += sb.toString();
	}

	/**
	 * 获取工具条
	 * 
	 * @author WU
	 * @date 2015-5-20 上午2:51:35
	 * @return
	 */
	private StringBuffer getToolbars() {
		StringBuffer sb = new StringBuffer("");
		if (buts.size() > 0 || querys.size() > 0) {
			sb.append("<div id='" + this.name + "Tb' style=\"display:none\">\n");
			sb.append(getButs());
			sb.append(getQuery());
			sb.append("</div>\n");
		}
		return sb;
	}

	private StringBuffer getButs() {
		StringBuffer sb = new StringBuffer("<table><tr>");
		for (int i = 0; i < buts.size(); i++) {
			// 按钮权限处理
			if (StringUtil.isNotEmpty(buts.get(i).getCode())) {
				if (ClientManager.getInstance().getClientSession().getButMap().containsKey(buts.get(i).getCode())) {
					sb.append("<td>");
					sb.append("<a href='javascript:void(0)' onclick=\"" + buts.get(i).getMethod() + "('" + this.name + "',");
					sb.append("'" + buts.get(i).getTitle() + "','" + buts.get(i).getUrl() + "'," + buts.get(i).getWidth() + "," + buts.get(i).getHeight() + ",'" + buts.get(i).getUserCode() + "'");
					sb.append(")\" class='easyui-linkbutton' plain='true' iconCls='" + buts.get(i).getIcon() + "'>" + buts.get(i).getName() + "</a>\n");
					sb.append("</td>");
					if (buts.size() - 1 != i || this.excel) {
						sb.append("<td><div class='datagrid-btn-separator'></div></td>");
					}
				}
			} else {
				sb.append("<td>");
				sb.append("<a href='javascript:void(0)' onclick=\"" + buts.get(i).getMethod() + "('" + this.name + "',");
				sb.append("'" + buts.get(i).getTitle() + "','" + buts.get(i).getUrl() + "'," + buts.get(i).getWidth() + "," + buts.get(i).getHeight() + ",'" + buts.get(i).getUserCode() + "'");
				sb.append(")\" class='easyui-linkbutton' plain='true' iconCls='" + buts.get(i).getIcon() + "'>" + buts.get(i).getName() + "</a>\n");
				sb.append("</td>");
				if (buts.size() - 1 != i || this.excel) {
					sb.append("<td><div class='datagrid-btn-separator'></div></td>");
				}
			}
		}
		if (this.excel) {
			String base = this.pageContext.getRequest().getServletContext().getContextPath();
			sb.append("<td>");
			sb.append("<a href='javascript:void(0)' onclick=\"exportExcel('" + base + "','" + this.name + "')\"");
			sb.append(" class='easyui-linkbutton' plain='true' iconCls='icon-export'>导出</a>\n");
			sb.append("</td>");
		}
		sb.append("</tr></table>");
		return sb;
	}

	/**
	 * 查询组装
	 * 
	 * @author WU
	 * @date 2015-5-20 下午2:12:31
	 * @return
	 */
	private StringBuffer getQuery() {
		StringBuffer sb = new StringBuffer("");
		if (querys != null && querys.size() > 0) {
			sb.append("<div style='padding:2px 5px;border-top: 1px dotted #dddddd;'>\n");
			for (int i = 0; i < querys.size(); i++) {
				sb.append(querys.get(i).getName() + ": ");

				if (querys.get(i).isSelect() || StringUtil.isNotEmpty(querys.get(i).getJson()) || querys.get(i).getCode() != null) {
					sb.append("<select '" + querys.get(i).getField() + "' name='" + querys.get(i).getField() + "'");
					if (StringUtil.isNotEmpty(querys.get(i).getStyle())) {
						sb.append(" style='" + querys.get(i).getStyle() + "'");
					}
					sb.append(" class='" + querys.get(i).getClassNmae() + "'>\n");
					sb.append("<option value=''>全部</option>\n");

					if (StringUtil.isNotEmpty(querys.get(i).getJson())) {
						List<options> opts = this.getOption(querys.get(i).getJson());
						for (int j = 0; j < opts.size(); j++) {
							if (StringUtil.isNotEmpty(querys.get(i).getValue()) && querys.get(i).getValue().equals(opts.get(j).key)) {
								sb.append("<option selected='selected' value='" + opts.get(j).key + "'>" + opts.get(j).value + "</option>\n");
							} else {
								sb.append("<option value='" + opts.get(j).key + "'>" + opts.get(j).value + "</option>\n");
							}
						}
					}

					if (querys.get(i).getCode() != null) {
						List<CodeEntity> codeList = ClientManager.getInstance().getCodeList(querys.get(i).getCode());
						if (codeList != null) {
							for (int j = 0; j < codeList.size(); j++) {
								if (StringUtil.isNotEmpty(querys.get(i).getValue()) && querys.get(i).getValue().equals(codeList.get(j).getValue())) {
									sb.append("<option selected='selected' value='" + codeList.get(j).getValue() + "'>" + codeList.get(j).getName() + "</option>\n");

								} else {
									sb.append("<option value='" + codeList.get(j).getValue() + "'>" + codeList.get(j).getName() + "</option>\n");
								}
							}
						}
					}
					sb.append("</select>\n");

				} else {
					if (querys.get(i).isExtent()) {// 区间查询
						sb.append("<input id='" + querys.get(i).getField() + "' name='" + querys.get(i).getField() + DATA_BEGIN + "'");
						if (StringUtil.isNotEmpty(querys.get(i).getStyle())) {
							sb.append(" style='" + querys.get(i).getStyle() + "'");
						}

						if (StringUtil.isNotEmpty(querys.get(i).getValue())) {
							sb.append(" value='" + querys.get(i).getValue() + "'");
						}
						sb.append(" class='" + querys.get(i).getClassNmae() + "'");
						sb.append(" type='" + querys.get(i).getType() + "'/><span style='font-family:helvetica'>~</span>");

						sb.append("<input id='" + querys.get(i).getField() + "' name='" + querys.get(i).getField() + DATA_END + "'");
						if (StringUtil.isNotEmpty(querys.get(i).getStyle())) {
							sb.append(" style='" + querys.get(i).getStyle() + "'");
						}

						if (StringUtil.isNotEmpty(querys.get(i).getValue())) {
							sb.append(" value='" + querys.get(i).getValue() + "'");
						}
						sb.append(" class='" + querys.get(i).getClassNmae() + "'");
						sb.append(" type='" + querys.get(i).getType() + "'/>\n");

					} else {
						sb.append("<input id='" + querys.get(i).getField() + "' name='" + querys.get(i).getField() + "'");
						if (StringUtil.isNotEmpty(querys.get(i).getStyle())) {
							sb.append(" style='" + querys.get(i).getStyle() + "'");
						}

						if (StringUtil.isNotEmpty(querys.get(i).getValue())) {
							sb.append(" value='" + querys.get(i).getValue() + "'");
						}
						sb.append(" class='" + querys.get(i).getClassNmae() + "'");
						sb.append(" type='" + querys.get(i).getType() + "'/>\n");
					}
				}
			}
			sb.append("<a href='#' style='margin:2px 20px;width:80px' class='easyui-linkbutton' iconCls='icon-search' onclick=" + name + "Query()>查询</a>");
			sb.append("</div>\n");
		}
		return sb;
	}

	private StringBuffer getEasyuiJs() {
		StringBuffer sb = new StringBuffer();
		sb.append("<script type=\"text/javascript\">\n");
		if (this.treegrid) {
			sb.append("$('#" + this.name + "').treegrid({\n");
			sb.append("treeField:'" + this.treeField + "',\n");
		} else {
			sb.append("$('#" + this.name + "').datagrid({\n");
		}
		if (StringUtil.isNotEmpty(idField)) {
			sb.append("idField:'" + this.idField + "',\n");
		}
		sb.append("method:'" + this.method + "',\n");

		sb.append("pagination:" + this.pagination + ",\n");
		if (this.pagination) {
			sb.append("pageSize:" + this.pageSize + ",\n");
			if (pageSize % 10 == 0 && pageSize > 10) {
				sb.append("pageList:[" + (pageSize - 10) + "," + pageSize + "," + (pageSize + 10) + "," + (pageSize + 20) + "],\n");
			} else if (pageSize < 10) {
				sb.append("pageList:[" + pageSize + "," + pageSize * 2 + "," + pageSize * 3 + "," + pageSize * 4 + "],\n");
			} else if (pageSize % 5 == 0 && pageSize > 10) {
				sb.append("pageList:[" + (pageSize - 5) + "," + pageSize + "," + (pageSize + 5) + "," + (pageSize + 10) + "],\n");
			} else {
				sb.append("pageList:[" + pageSize + "," + pageSize * 2 + "," + pageSize * 3 + "],\n");
			}
		}
		if (StringUtil.isNotEmpty(sortName)) {
			sb.append("sortName:'" + this.sortName + "',\n");
		}
		if (StringUtil.isNotEmpty(sortOrder)) {
			sb.append("sortOrder:'" + this.sortOrder + "',\n");
		}
		if (StringUtil.isNotEmpty(title)) {
			sb.append("title:'" + this.title + "',\n");
		}
		if (StringUtil.isNotEmpty(url)) {
			sb.append("url:'" + this.url + "',\n");
		}

		if (StringUtil.isNotEmpty(toolbar)) {
			sb.append("toolbar:'" + this.toolbar + "',\n");

		} else if (buts.size() > 0 || querys.size() > 0) {
			sb.append("toolbar:'#" + this.name + "Tb',\n");
		}

		sb.append("fit:" + this.fit + ",\n");
		if (!this.fit) {
			if (this.width != null) {
				sb.append("width:" + this.width + ",\n");
			}
			if (this.height != null) {
				sb.append("height:" + this.height + ",\n");
			}
		}
		if (showFooter) {
			sb.append("showFooter:" + showFooter + ",\n");
		}
		sb.append("fitColumns:" + this.fitColumns + ",\n");
		sb.append("rownumbers:" + this.rownumbers + ",\n");
		sb.append("border:" + this.border + ",\n");
		sb.append("singleSelect:" + this.singleSelect + ",\n");
		sb.append("striped:" + this.striped + ",\n");

		if (StringUtil.isNotEmpty(queryParams)) {
			sb.append("queryParams:{" + (param == null ? "" : param) + "excel:" + this.excel + ",tagId:'" + this.name + "',fields : '" + this.fields + "'," + queryParams + "},\n");
		} else {
			sb.append("queryParams:{" + (param == null ? "" : param) + "excel:" + this.excel + ",tagId:'" + this.name + "',fields : '" + this.fields + "'},\n");
		}

		if (StringUtil.isNotEmpty(extendProperties)) {
			sb.append(this.extendProperties + ",\n");
		}

		if (StringUtil.isNotEmpty(this.onBeforeLoad)) {
			sb.append("onBeforeLoad:" + this.onBeforeLoad + ",\n");
		}
		if (StringUtil.isNotEmpty(this.onClickCell)) {
			sb.append("onClickCell:" + this.onClickCell + ",\n");
		}
		if (StringUtil.isNotEmpty(this.rowStyler)) {
			sb.append("rowStyler:" + this.rowStyler + ",\n");
		}
		if (StringUtil.isNotEmpty(this.onClickRow)) {
			sb.append("onClickRow:function(index,row){return " + this.onClickRow + "(index,row);},\n");
		}
		if (StringUtil.isNotEmpty(this.onDblClickCell)) {
			sb.append("onDblClickCell:" + this.onDblClickCell + ",\n");
		}
		if (StringUtil.isNotEmpty(this.onDblClickRow)) {
			sb.append("onDblClickRow:" + this.onDblClickRow + ",\n");
		}

		if (StringUtil.isNotEmpty(this.onLoadSuccess)) {
			sb.append("onLoadSuccess:" + this.onLoadSuccess + ",\n");
		}
		if (StringUtil.isNotEmpty(this.extendFunction)) {
			sb.append(this.extendFunction + ",\n");
		}

		sb.append("onLoadError:function(e){\n");
		sb.append(" var resText = e.responseText;\n");
		sb.append(" if(resText=='{sessionState:0}'){\n");
		sb.append("   $.messager.alert('系统提示','用户会话过期或者权限认证失败，请重新登陆后再试！','error');\n");
		sb.append(" }else{\n");
		sb.append("   error('数据加载失败:'+resText)\n");
		sb.append(" }\n");
		sb.append("},\n");

		sb.append("loadMsg :'数据加载中,请稍后...'\n");
		sb.append("});\n");

		if (this.opts.size() > 0) {
			sb.append("function formatterOpt(value,row){\n");
			sb.append("if(row){\n");
			sb.append("var html=\"\";\n");
			for (int i = 0; i < this.opts.size(); i++) {
				if (StringUtil.isNotEmpty(opts.get(i).getCode())) {
					if (ClientManager.getInstance().getClientSession().getButMap().containsKey(opts.get(i).getCode())) {
						sb.append(this.getOpt(opts.get(i)));
					}
				} else {
					sb.append(this.getOpt(opts.get(i)));
				}
			}
			sb.append("return html;\n");
			sb.append("};\n");
			sb.append("}\n");
		}

		if (querys.size() > 0) {
			sb.append("function " + name + "Query(){\n");
			sb.append(" var param=new Object();\n");
			sb.append(" param.fields='" + fields + "';\n");
			if (this.excel) {
				sb.append(" param.tagId='" + this.name + "';\n");
			}
			sb.append("$('#" + name + "Tb input[name],#" + name + "Tb select[name]').each(function(){\n");
			sb.append("  if(this.value){param[this.name]=this.value;}\n");
			sb.append("});\n");
			if (this.treegrid) {
				sb.append(" $('#" + name + "').treegrid('clearSelections');\n");
				sb.append(" $('#" + name + "').treegrid('load',param);\n");
			} else {
				sb.append(" $('#" + name + "').datagrid('clearSelections');\n");
				sb.append(" $('#" + name + "').datagrid('load',param);\n");
			}
			sb.append("}\n");
		}

		if (StringUtil.isNotEmpty(function)) {
			sb.append(this.function);
		}
		sb.append(" function refreshGrid(){\n");
		if (this.treegrid) {
			sb.append(" $('#" + name + "').treegrid('clearSelections');\n");
			sb.append(" $('#" + name + "').treegrid('reload');\n");
		} else {
			sb.append(" $('#" + name + "').datagrid('clearSelections');\n");
			sb.append(" $('#" + name + "').datagrid('reload');\n");
		}
		sb.append("}\n");

		sb.append("$(document).on('keydown',function(event) {\n");
		sb.append(" if (event.keyCode == 13) {\n");
		sb.append("  " + name + "Query();\n");
		sb.append("  return false;\n");
		sb.append(" }\n");
		sb.append("});\n");

		sb.append("</script>");
		return sb;
	}

	private StringBuffer getOpt(DataGridOptTag opt) {
		StringBuffer sb = new StringBuffer("");
		if (StringUtil.isNotEmpty(opt.getExp())) {
			String name = opt.getExp().substring(0, opt.getExp().indexOf("#"));
			String exp = opt.getExp().substring(opt.getExp().indexOf("#") + 1, opt.getExp().length());
			sb.append("if(row['" + name + "']" + exp + "){\n");
			sb.append("html+=\"<a href=\\\"#\\\" class=\\\"opt\\\" ");
			sb.append("onclick=\\\"" + opt.getMethod() + "('\"+row['" + opt.getValue() + "']+\"',");
			sb.append("'" + opt.getTitle() + "','" + opt.getUrl() + "',");
			sb.append(opt.getWidth() + "," + opt.getHeight());
			sb.append(")\\\">[" + opt.getName() + "]</a>\";\n");
			sb.append("}\n");
		} else {
			sb.append("html+=\"<a href=\\\"#\\\" class=\\\"opt\\\" ");
			sb.append("onclick=\\\"" + opt.getMethod() + "('\"+row['" + opt.getValue() + "']+\"',");
			sb.append("'" + opt.getTitle() + "','" + opt.getUrl() + "',");
			sb.append(opt.getWidth() + "," + opt.getHeight());
			sb.append(")\\\">[" + opt.getName() + "]</a>\";\n");
		}
		return sb;

	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getIdField() {
		return idField;
	}

	public void setIdField(String idField) {
		this.idField = idField;
	}

	public Integer getPageSize() {
		return pageSize;
	}

	public void setPageSize(Integer pageSize) {
		this.pageSize = pageSize;
	}

	public boolean isFit() {
		return fit;
	}

	public void setFit(boolean fit) {
		this.fit = fit;
	}

	public boolean isBorder() {
		return border;
	}

	public void setBorder(boolean border) {
		this.border = border;
	}

	public boolean isPagination() {
		return pagination;
	}

	public void setPagination(boolean pagination) {
		this.pagination = pagination;
	}

	public boolean isRownumbers() {
		return rownumbers;
	}

	public void setRownumbers(boolean rownumbers) {
		this.rownumbers = rownumbers;
	}

	public boolean isSingleSelect() {
		return singleSelect;
	}

	public void setSingleSelect(boolean singleSelect) {
		this.singleSelect = singleSelect;
	}

	public boolean isFitColumns() {
		return fitColumns;
	}

	public void setFitColumns(boolean fitColumns) {
		this.fitColumns = fitColumns;
	}

	public boolean isShowFooter() {
		return showFooter;
	}

	public void setShowFooter(boolean showFooter) {
		this.showFooter = showFooter;
	}

	public boolean isStriped() {
		return striped;
	}

	public void setStriped(boolean striped) {
		this.striped = striped;
	}

	public String getMethod() {
		return method;
	}

	public void setMethod(String method) {
		this.method = method;
	}

	public String getSortName() {
		return sortName;
	}

	public void setSortName(String sortName) {
		this.sortName = sortName;
	}

	public String getSortOrder() {
		return sortOrder;
	}

	public void setSortOrder(String sortOrder) {
		this.sortOrder = sortOrder;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getToolbar() {
		return toolbar;
	}

	public void setToolbar(String toolbar) {
		this.toolbar = toolbar;
	}

	public String getQueryParams() {
		return queryParams;
	}

	public void setQueryParams(String queryParams) {
		this.queryParams = queryParams;
	}

	public String getExtendProperties() {
		return extendProperties;
	}

	public void setExtendProperties(String extendProperties) {
		this.extendProperties = extendProperties;
	}

	public String getOnLoadSuccess() {
		return onLoadSuccess;
	}

	public void setOnLoadSuccess(String onLoadSuccess) {
		this.onLoadSuccess = onLoadSuccess;
	}

	public String getOnBeforeLoad() {
		return onBeforeLoad;
	}

	public void setOnBeforeLoad(String onBeforeLoad) {
		this.onBeforeLoad = onBeforeLoad;
	}

	public String getOnClickRow() {
		return onClickRow;
	}

	public void setOnClickRow(String onClickRow) {
		this.onClickRow = onClickRow;
	}

	public String getOnDblClickRow() {
		return onDblClickRow;
	}

	public void setOnDblClickRow(String onDblClickRow) {
		this.onDblClickRow = onDblClickRow;
	}

	public String getOnClickCell() {
		return onClickCell;
	}

	public void setOnClickCell(String onClickCell) {
		this.onClickCell = onClickCell;
	}

	public String getOnDblClickCell() {
		return onDblClickCell;
	}

	public void setOnDblClickCell(String onDblClickCell) {
		this.onDblClickCell = onDblClickCell;
	}

	public String getExtendFunction() {
		return extendFunction;
	}

	public void setExtendFunction(String extendFunction) {
		this.extendFunction = extendFunction;
	}

	public Integer getHeight() {
		return height;
	}

	public void setHeight(Integer height) {
		this.height = height;
	}

	public Integer getWidth() {
		return width;
	}

	public void setWidth(Integer width) {
		this.width = width;
	}

	public boolean isCheckbox() {
		return checkbox;
	}

	public void setCheckbox(boolean checkbox) {
		this.checkbox = checkbox;
	}

	public String getRowStyler() {
		return rowStyler;
	}

	public void setRowStyler(String rowStyler) {
		this.rowStyler = rowStyler;
	}

	public boolean isTreegrid() {
		return treegrid;
	}

	public void setTreegrid(boolean treegrid) {
		this.treegrid = treegrid;
	}

	public void setColumn(DataGridRowTag column) {
		if (column.isQuery()) {
			DataGridQueryTag query = new DataGridQueryTag();
			try {
				BeanUtils.copyProperties(column, query);
			} catch (Exception e) {
				e.printStackTrace();
			}
			this.setQuery(query);
		}
		DataGridRowTag rowTag = new DataGridRowTag();
		try {
			BeanUtils.copyProperties(column, rowTag);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (this.fields == null) {
			this.fields = column.getField();

		} else {
			this.fields += "," + column.getField();

		}
		if (column.isFrozen()) {
			this.frozens.add(rowTag);

		} else {
			this.row.add(rowTag);

		}
	}

	public void setToolbars(DataGridButTag tool) {
		DataGridButTag toolbar = new DataGridButTag();
		try {
			BeanUtils.copyProperties(tool, toolbar);
		} catch (Exception e) {
			e.printStackTrace();
		}
		this.buts.add(toolbar);

	}

	public void setQuery(DataGridQueryTag query) {
		if (StringUtil.isNotEmpty(query.getValue())) {
			if (param == null) {
				param = query.getName() + ":'" + query.getValue() + "',";
			} else {
				param += query.getName() + ":'" + query.getValue() + "',";
			}
		}
		DataGridQueryTag q = new DataGridQueryTag();
		try {
			BeanUtils.copyProperties(query, q);
		} catch (Exception e) {
			e.printStackTrace();
		}
		this.querys.add(q);

	}

	public List<options> getOption(String str) {
		JSONObject obj = JSONObject.parseObject(str);
		List<options> ops = new ArrayList<options>();
		Set<?> it = obj.keySet();
		for (Object key : it) {
			Object value = obj.get(key);
			ops.add(new options(key.toString(), value.toString()));
		}
		return ops;
	}

	public String getTreeField() {
		return treeField;
	}

	public void setTreeField(String treeField) {
		this.treeField = treeField;
	}

	public void setOpts(DataGridOptTag opt) {
		DataGridOptTag o = new DataGridOptTag();
		try {
			BeanUtils.copyProperties(opt, o);
		} catch (Exception e) {
			e.printStackTrace();
		}
		this.opts.add(o);
	}

	public boolean isExcel() {
		return excel;
	}

	public void setExcel(boolean excel) {
		this.excel = excel;
	}

	/**
	 * 
	 * @author WU
	 * @date 2015-5-20 下午11:19:41
	 */
	class options {
		public String key;
		public String value;

		public options(String key, String value) {
			this.key = key;
			this.value = value;
		}

	}
}
